package com.asiainfo.iam.producer;

import com.alibaba.fastjson.JSON;
import com.asiainfo.iam.config.RocketMQConfig;
import com.asiainfo.iam.constant.MQConstant;
import com.asiainfo.iam.entity.dto.FullSyncRequest;
import com.asiainfo.iam.entity.vo.MessageBase;
import com.asiainfo.iam.entity.vo.Organization;
import com.asiainfo.iam.entity.vo.Role;
import com.asiainfo.iam.entity.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
// TODO 4
@ConditionalOnProperty(name = "enable.syn.controller", havingValue = "true")
public class FullSyncProducer {

    private static final Logger log = LoggerFactory.getLogger(FullSyncProducer.class);
    @Autowired
    private RocketMQConfig rocketMQConfig;


    /**
     * 发送全量消息
     */
    public <T> void sendMessage(String objectType, T data, String operateType) throws Exception {
        //封装消息
        MessageBase<T> msg = new MessageBase<>();
        msg.setMsgObject(objectType);
        msg.setMsgId(UUID.randomUUID().toString());
        msg.setMsgOperateType(operateType);
        msg.setMsgTime(System.currentTimeMillis());
        msg.setMsgData(data);

        String msgBody = JSON.toJSONString(msg);
        rocketMQConfig.sendFullMessage(MQConstant.FULL_SYNC_TAG, msgBody);
        log.debug("发送增量消息: 对象类型={} - 操作类型{} - 消息内容{}", objectType, operateType, msgBody);
    }

    /**
     * 全量数据顺序发送
     * @param request
     * @throws Exception
     */
    public void syncFullData(FullSyncRequest request) throws Exception {
        // 从包装类中获取数据
        List<Organization> orgs = request.getOrganizations();
        List<Role> roles = request.getRoles();
        List<User> users = request.getUsers();

        //1.先同步组织数据
        syncOrganizations(orgs);
        // 2. 再同步角色数据
        syncRoles(roles);
        // 3. 最后同步用户数据
        syncUsers(users);
    }


    /**
     * 同步组织数据
     */
    private void syncOrganizations(List<Organization> orgs) throws Exception {
        if (orgs == null || orgs.isEmpty()){
            return;
        }
        //遍历组织数据
        for (Organization org : orgs) {
            sendMessage(MQConstant.OBJECT_ORG, org, MQConstant.OPERATE_ADD);
            log.info("组织数据发送成功！");
        }
    }
    /**
     * 同步角色数据
     */
    private void syncRoles(List<Role> roles) throws Exception {
        if (roles == null || roles.isEmpty()){
            return;
        }
        for (Role role : roles) {
            sendMessage(MQConstant.OBJECT_ROLE, role, MQConstant.OPERATE_ADD);
            log.info("角色数据发送成功！");
        }
    }
    /**
     * 同步用户数据
     */
    private void syncUsers(List<User> users) throws Exception {
        if (users == null || users.isEmpty()){
            return;
        }
        for (User user : users) {
            sendMessage(MQConstant.OBJECT_USER, user, MQConstant.OPERATE_ADD);
            log.info("用户数据发送成功！");
        }
    }
}
